<?php
App::uses('AuthComponent', 'Controller/Component');
/**
 * User Model
 *
 * @property Document $Document
 */
class User extends AppModel {

public function beforeSave($options = array()) {
		if (isset($this->data[$this->alias]['password'])) {
			$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
		}
		return true;
	}

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'title' => array(
			'allowEmpty' => true,
			'last' => false,
			'rule' => array('maxLength', 12),
		), 
		'first_name' => array(
			'rule' => array('maxLength', 100),
			'notempty' => array(
				'rule' => array('notempty'),
			),
		),
		'middle_name' => array(
			'allowEmpty' => true,
			'last' => false,
			'rule' => array('maxLength', 100),
		),
		'surname' => array(
			'rule' => array('maxLength', 100),
			'notempty' => array(
				'rule' => array('notempty'),
			),
		),
		'email' => array(
			'rule' => array('maxLength', 50),
			'notempty' => array(
				'rule' => array('notempty'),
			),
			'isUnique' => array(
				'rule' => array('checkUniqueEmail'),
				'message' => 'This email address is already in use.'),
		),
		'password' => array(
			'rule' => array('maxLength', 16),
			'notempty' => array(
				'rule' => array('notempty'),
			),
		),
		'password_confirm' => array(
			'required'=>'notEmpty',
			'match'=>array(
			'rule' => 'validatePasswdConfirm',
			'message' => 'Passwords do not match'
			)
		),
		'address' => array(
			'allowEmpty' => true,
			'last' => false,
			'rule' => array('maxLength', 100),
		),
		'suburb' => array(
			'allowEmpty' => true,
			'last' => false,
			'rule' => array('maxLength', 100),
		),
		'state' => array(
			'allowEmpty' => true,
			'last' => false,
			'rule' => array('maxLength', 26),
		),
		'postcode' => array(
			'allowEmpty' => true,
			'last' => false,
			'rule' => array('maxLength', 15),
		),
		'country' => array(
			'rule' => array('maxLength', 26),
			'notempty' => array(
				'rule' => array('notempty'),
			),
		),
		'landline_number' => array(
			'allowEmpty' => true,
			'last' => false,
			'rule' => array('maxLength', 50),
		),
		'mobile_number' => array(
			'allowEmpty' => true,
			'last' => false,
			'rule' => array('maxLength', 50),
		),
	);
	
	function validatePasswdConfirm($data)
	{
		if ($this->data['User']['password'] !== $data['password_confirm'])
		{
		  return false;
		}
		return true;
	}
	
	function checkUniqueEmail()
	{
		return ($this->find('count', array('conditions' => array('User.email' => $this->data['User']['email']))) == 0);
	}
	
	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Document' => array(
		'className' => 'Document',
		'foreignKey' => 'user_id',
		'dependent' => false,
		'conditions' => '',
		'fields' => '',
		'order' => '',
		'limit' => '',
		'offset' => '',
		'exclusive' => '',
		'finderQuery' => '',
		'counterQuery' => ''
	));
	
	public $hasOne = array(
		'Enquiry' => array(
		'className' => 'Enquiry',
		'foreignKey' => 'user_id',
		'dependent' => false,
		'conditions' => '',
		'fields' => '',
		'order' => '',
		'limit' => '',
		'offset' => '',
		'exclusive' => '',
		'finderQuery' => '',
		'counterQuery' => ''
	));
	
	
		
	public $belongsTo = array(
		'Role' => array(
		'className' => 'Role',
		'foreignKey' => 'role',
		'conditions' => '',
		'fields' => '',
		'order' => '') ,
		'Country' => array(
		'className' =>'Country',
		'foreignKey' => 'country',
		'conditions' => '',
		'fields' => '',
		'order' => '') 
	); 
}
